﻿using Microsoft.EntityFrameworkCore;
using StarsAdmin.Core.DB.Entities;
using System.Reflection;

namespace StarsAdmin.Core.DB
{
    public class DBContext : DbContext
    {
        //private static readonly ILoggerFactory _loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());

        public DBContext(DbContextOptions<DBContext> options) : base(options)
        {
            //这句加上后，EF Core会根据实体模型自动生成数据库
            //this.Database.EnsureCreatedAsync();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            //optionsBuilder.AddInterceptors(new DbCommandCustomInterceptor()); // sql执行拦截器
            //optionsBuilder.UseLoggerFactory(_loggerFactory); // 默认就有这个配置
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
        }

        public DbSet<UserEntity> Users { get; set; }
        public DbSet<RoleEntity> Roles { get; set; }
        public DbSet<MenuEntity> Menus { get; set; }
        public DbSet<UserRoleEntity> UserRoles { get; set; }
        public DbSet<RoleMenuEntity> RoleMenus { get; set; }
        public DbSet<MenuApiEntity> MenuApis { get; set; }

        public DbSet<ApiEntity> Apis { get; set; }
        public DbSet<FileEntity> Files { get; set; }
        public DbSet<DictEntity> Dicts { get; set; }
        public DbSet<DictTypeEntity> DictTypes { get; set; }
        public DbSet<RegionEntity> Regions { get; set; }

        public DbSet<LoginLogEntity> LoginLogs { get; set; }
        public DbSet<OperationLogEntity> OprationLogs { get; set; }
    }
}